package hua.mtreader.main;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import hua.mtreader.R;
import hua.mtreader.feature.bookshelf.fragment.BookshelfFragment;
import hua.mtreader.feature.choiceness.fragment.ChoicenessFragment;
import hua.mtreader.feature.personal.fragment.PersonalFragment;

public class HomeActivity extends AppCompatActivity {

    private FrameLayout flContent;
    private RadioGroup rgBottom;
    private RadioButton rbBookshel;
    private RadioButton rbChoiceness;
    private RadioButton rbPersonal;
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        initView();
        setListener();
        initData();
    }


    private void initView() {
        flContent = (FrameLayout) findViewById(R.id.fl_content);
        rgBottom = (RadioGroup) findViewById(R.id.rg_bottom);
        rbBookshel = (RadioButton) findViewById(R.id.rb_bookshelf);
        rbChoiceness = (RadioButton) findViewById(R.id.rb_choiceness);
        rbPersonal = (RadioButton) findViewById(R.id.rb_personal);
    }

    private void setListener() {

        rgBottom.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_bookshelf:
                        index = 0;
                        break;
                    case R.id.rb_choiceness:
                        index = 1;
                        break;
                    case R.id.rb_personal:
                        index = 2;
                        break;
                }

                // 具体实现就是调用了数据适配器的getItem(int arg0);
                Fragment fragment = (Fragment) fragmentStatePagerAdapter.instantiateItem(flContent, index);
                // 第三个参数才有意义,就是去替换帧布局的fragment对象
                fragmentStatePagerAdapter.setPrimaryItem(null, 0, fragment);
                // 提交
                fragmentStatePagerAdapter.finishUpdate(null);
            }
        });

    }

    private void initData() {
        rgBottom.check(R.id.rb_bookshelf);
    }

    FragmentStatePagerAdapter fragmentStatePagerAdapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch(position){
                case 0:
                    fragment = new BookshelfFragment();
                    break;
                case 1:
                    fragment = new ChoicenessFragment();
                    break;
                case 2:
                    fragment = new PersonalFragment();
                    break;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 3;
        }
    };



}
